home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / LLIST.ZIP / STRINGS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-26  |  9.7 KB  |  372 lines

  1. /*------------------------------------------------------------
  2. | FILE NAME: Strings.c
  3. |
  4. | DOCUMENT: [1016.0]
  5. |
  6. | PURPOSE: To provide a system of string manipulation functions.
  7. |
  8. | DESCRIPTION: 
  9. |
  10. | NOTE: See 'DynString.c' for dynamically allocated string 
  11. |       functions.
  12. |
  13. |       See 'Parse.c' for string parsing functions.
  14. |
  15. | HISTORY:  02.03.93 from xstrings.c.
  16. |           11.08.93 moved parsing functions to 'Parse.c'
  17. ------------------------------------------------------------*/
  18.  
  19. #define lower(c)    (isupper(c) ? tolower(c) : (c))
  20.  
  21. #include <ctype.h>
  22. #include <Strings.h>
  23.  
  24. /*------------------------------------------------------------
  25. | NAME: AppendString
  26. |
  27. | PURPOSE: To append a string to another.
  28. |
  29. | DESCRIPTION: 
  30. |
  31. | EXAMPLE:  AppendString( AString, SuffixString );
  32. |
  33. | NOTE: 
  34. |
  35. | ASSUMES: String is terminated with a 0.
  36. |
  37. | HISTORY: 02.01.89 by Lee Malone
  38. ------------------------------------------------------------*/
  39. Nothing
  40. AppendString( AddressOfString AString, 
  41.               AddressOfString SuffixString )
  42. {
  43.     CopyString( SuffixString, 
  44.                 AString + CountString( AString ) );
  45. }
  46.  
  47. /*------------------------------------------------------------
  48. | NAME: AppendStrings
  49. |
  50. | PURPOSE: To append a variable number of strings together.
  51. |
  52. | DESCRIPTION: 
  53. |
  54. | EXAMPLE:  
  55. |
  56. |     String  ABuffer[255];        
  57. |                                  
  58. |     CopyString( "This", ABuffer );
  59. |                                  
  60. |     AppendStrings( ABuffer,  <-- destination string
  61. |                    " is",    <-- AddressOf suffix string
  62. |                    " a",     <-- AddressOf suffix string
  63. |                    " test.", <-- AddressOf suffix string
  64. |       (AddressOfString) 0 ); <-- 0 terminates parameter list
  65. |                                 
  66. |                                  
  67. |       yields: "This is a test."
  68. |
  69. |
  70. | NOTE: The terminating 0 parameter must be cast as 
  71. |       'AddressOfString' to prevent errors.
  72. |
  73. | ASSUMES: String is terminated with a 0.
  74. |          Destination buffer is large enough to hold result.
  75. |          Parameters are compiled in 
  76. |              left-to-right:low-to-high-memory order.
  77. |
  78. | HISTORY: 12.06.89 by Lee Malone from source by Jack A. Zucker
  79. ------------------------------------------------------------*/
  80. Nothing
  81. AppendStrings( AddressOfString AString, 
  82.                AddressOfString SuffixString, ... ) 
  83. {
  84.    AddressOfAddressOfString  ParameterPointer;
  85.  
  86.    ParameterPointer = &SuffixString;
  87.  
  88.    while( *ParameterPointer )
  89.    {
  90.        AppendString( AString, *ParameterPointer );
  91.        ParameterPointer++;
  92.    }
  93. }
  94.  
  95. /*------------------------------------------------------------
  96. | NAME: CompareStrings
  97. |
  98. | PURPOSE: To compare two strings based on their ordering in 
  99. | the alphabet, independent of upper/lower case.
  100. |
  101. | DESCRIPTION: Comparison operation.
  102. |              Returns: 0 if string AA = string BB.
  103. |                       positive number if AA > BB.
  104. |                       negative number if AA < BB.
  105. |
  106. | Compared until end of either string or until an in-equality 
  107. | is detected.
  108. |
  109. | EXAMPLE:  Result = CompareStrings( Name, "John Galt" );
  110. |
  111. |
  112. | NOTE: 
  113. |
  114. | ASSUMES: String is terminated with a 0.
  115. |
  116. | HISTORY:  01.11.89 by Lee Malone
  117. |           04.04.91 removed case sensitive comparison
  118. |           11.11.93 changed name from 
  119. |                    'CompareStringsLexigraphic' 
  120. |                    
  121. ------------------------------------------------------------*/
  122. Comparison
  123. CompareStrings( AddressOfString A, 
  124.                 AddressOfString B)
  125. {
  126.     Comparison  Result;
  127.     
  128.     while(1)
  129.     {
  130.         Result = (Comparison) (lower(*A) - lower(*B));
  131.         
  132.         /* return if unequal or end of either string */
  133.         
  134.         if(Result || *A == 0 || *B == 0)
  135.         {
  136.             return(Result);   
  137.         }
  138.         A++;
  139.         B++;
  140.     }
  141. }
  142.  
  143. /*------------------------------------------------------------
  144. | NAME: ConvertStringToLowerCase
  145. |
  146. | PURPOSE: To convert a string in place to lower case.
  147. |
  148. | DESCRIPTION: 
  149. |
  150. | EXAMPLE:  ConvertStringToLowerCase( AString );
  151. |
  152. | NOTE: 
  153. |
  154. | ASSUMES: String is terminated with a 0.
  155. |
  156. | HISTORY:  03.25.91 by Lee Malone 
  157. |           11.11.93 fixed 'isupper' error
  158. ------------------------------------------------------------*/
  159. Nothing
  160. ConvertStringToLowerCase(AddressOfString AString)
  161. {
  162.     while(*AString != 0)
  163.     {
  164.         if(isupper(*AString))
  165.         {
  166.             *AString = tolower((int)*AString);
  167.         }
  168.         AString++;
  169.     }
  170. }
  171.  
  172. /*------------------------------------------------------------
  173. | NAME: ConvertStringToUpperCase
  174. |
  175. | PURPOSE: To convert a string in place to upper case.
  176. |
  177. | DESCRIPTION: 
  178. |
  179. | EXAMPLE:  ConvertStringToUpperCase( AString );
  180. |
  181. | NOTE: 
  182. |
  183. | ASSUMES: String is terminated with a 0.
  184. |
  185. | HISTORY: 03.22.89 by Lee Malone 
  186. ------------------------------------------------------------*/
  187. Nothing
  188. ConvertStringToUpperCase(AddressOfString    AString)
  189. {
  190.     while(*AString != 0)
  191.     {
  192.         if(islower(*AString))
  193.         {
  194.             *AString = toupper((int)*AString);
  195.         }
  196.         AString++;
  197.     }
  198. }
  199.  
  200. /*------------------------------------------------------------
  201. | NAME: CopyString
  202. |
  203. | PURPOSE: To copy a zero-delimited string from one place 
  204. | to another including the delimiter.
  205. |
  206. | DESCRIPTION: 
  207. |
  208. | EXAMPLE:  CopyString(FromString,ToBuffer);
  209. |
  210. | NOTE:  
  211. |
  212. | ASSUMES: Source and Target buffers are non-overlapping.
  213. |
  214. | HISTORY: 11.14.89 by Lee Malone
  215. |          02.15.93 changed to quad count.
  216. |          11.01.93 return value removed; now buffers must
  217. |                   not overlap.
  218. ------------------------------------------------------------*/
  219. Nothing
  220. CopyString( AddressOfString Source, 
  221.             AddressOfString Target )
  222. {
  223.     while( *Source != 0 )
  224.     {
  225.         *Target++ = *Source++;
  226.     }
  227.     *Target = 0;
  228. }
  229.  
  230. /*------------------------------------------------------------
  231. | NAME: CountString
  232. |
  233. | PURPOSE: To count the data bytes in a 0-terminated string. 
  234. |
  235. | DESCRIPTION:  Terminating 0 is not included in the count.
  236. |
  237. | EXAMPLE:  ByteCount = CountString( MyString );
  238. |
  239. | NOTE:  
  240. |
  241. | ASSUMES: 
  242. |
  243. | HISTORY: 08.31.89 by Lee Malone
  244. |          02.15.93 changed to return Quad instead of Pair.
  245. ------------------------------------------------------------*/
  246. Quad
  247. CountString( AddressOfString AString )
  248. {
  249.     Quad        ByteCount;
  250.  
  251.     ByteCount = 0;
  252.  
  253.     while( AString[ByteCount] != 0 )
  254.     {
  255.         ByteCount++;
  256.     }
  257.     
  258.     return( ByteCount );
  259. }
  260.  
  261. /*------------------------------------------------------------
  262. | NAME: FindLastByteInString
  263. |
  264. | PURPOSE: To find the address of the last byte in a string.
  265. |
  266. | DESCRIPTION: Returns the address of the byte immediately
  267. |              prior to the first 0 byte.  Empty strings
  268. |              return the address of the byte prior to where
  269. |              the first byte would be found.
  270. |
  271. | EXAMPLE:  LastByteAddress = FindLastByteInString(AString);
  272. |
  273. | NOTE: 
  274. |
  275. | ASSUMES: String is terminated with a 0.
  276. |
  277. | HISTORY: 02.01.89 by Lee Malone
  278. ------------------------------------------------------------*/
  279. AddressOfString
  280. FindLastByteInString(AddressOfString AString)
  281. {
  282.     while (*AString != 0) ++AString;
  283.  
  284.     AString--;
  285.  
  286.     return(AString);
  287. }
  288.  
  289. /*------------------------------------------------------------
  290. | NAME: InsertString
  291. |
  292. | PURPOSE: To insert a string into another string.
  293. |
  294. | DESCRIPTION:  
  295. |
  296. | EXAMPLE:  
  297. |      String  ABuffer[255];        
  298. |                                  
  299. |      CopyString( "This a test", ABuffer );
  300. |                                  
  301. |      InsertString( "is ", ABuffer, 5 );
  302. |                                  
  303. |      yields: "this is a test"
  304. |
  305. | NOTE:  
  306. |
  307. | ASSUMES: There is room in the destination buffer for the 
  308. |          new insertion.
  309. |
  310. | HISTORY: 12.06.89 by Lee Malone from source by Jack A. Zucker
  311. |          02.15.93 changed offset to quad.
  312. ------------------------------------------------------------*/
  313. Nothing
  314. InsertString( AddressOfString  SourceString, 
  315.               AddressOfString  DestinationString, 
  316.               Quad             DestinationByteOffset )
  317. {
  318.    Quad   SourceByteCount;
  319.  
  320.    SourceByteCount = CountString( SourceString );
  321.  
  322.    /* Make room for the new string. */
  323.    CopyString( (AddressOfString) DestinationString+
  324.                                  DestinationByteOffset, 
  325.                (AddressOfString) DestinationString+
  326.                                  DestinationByteOffset+
  327.                                  SourceByteCount );
  328.  
  329.    /* Put the new string in place. */
  330.    CopyBytes( (AddressOfByte) SourceString, 
  331.               (AddressOfByte) DestinationString+
  332.                               DestinationByteOffset, 
  333.               SourceByteCount );
  334. }
  335.  
  336. /*------------------------------------------------------------
  337. | NAME: ReplaceBytesInString
  338. |
  339. | PURPOSE: To replace all occurances of a byte within a 
  340. | string with another.
  341. |
  342. | DESCRIPTION:  
  343. |
  344. | EXAMPLE:  ReplaceBytesInString( AString, 
  345. |                                 (Pair) "a", 
  346. |                                 (Pair) "A" );
  347. |
  348. | NOTE: 'Pair' used as parameter instead of 'Byte' because
  349. |       Think C can't pass 'Byte' parameters properly. See
  350. |       'DataSize.h' for more. 
  351. |
  352. | ASSUMES: 
  353. |
  354. | HISTORY: 04.04.91 by Lee Malone
  355. |          11.11.93 changed 'Byte' parameters to 'Pair'.
  356. ------------------------------------------------------------*/
  357. Nothing
  358. ReplaceBytesInString( AddressOfString AString, 
  359.                       Pair From, 
  360.                       Pair To )
  361. {
  362.     while(*AString)
  363.     {
  364.         if( *AString == (String) From ) 
  365.         {
  366.             *AString = (String) To;
  367.         }
  368.         AString++;
  369.     }
  370. }
  371.  
  372.